package testview.zhen.com.myapplication;

import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Test;
import org.junit.runner.RunWith;

import androidx.test.runner.AndroidJUnit4;
import testview.zhen.com.myapplication.design.consumer_producer.BlockingQueueModel;
import testview.zhen.com.myapplication.design.consumer_producer.LockConditionModel;
import testview.zhen.com.myapplication.design.consumer_producer.LockConditionModel2;
import testview.zhen.com.myapplication.design.consumer_producer.Model;
import testview.zhen.com.myapplication.design.consumer_producer.WaitNotifyModel;
import testview.zhen.com.myapplication.tool.LockCountDemo;

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
//        Context appContext = InstrumentationRegistry.getTargetContext();

//        assertEquals("testview.zhen.com.myapplication", appContext.getPackageName());
    }
    @Test
    public void lock(){

        final LockCountDemo demo = new LockCountDemo();

        Thread th1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() +
                        " : 开始执行");
                demo.tmpAns1 = demo.add(demo.middle,demo.end+1);
            }
        },"count1");
        Thread th2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() +
                        " : 开始执行");
                demo.tmpAns2 = demo.add(demo.middle, demo.end + 1);
                System.out.println(Thread.currentThread().getName() +
                        " : calculate ans: " + demo.tmpAns2);
            }
        }, "count2");
        Thread th3 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName() +
                            " : 开始执行");
                    int ans = demo.sum();
                    System.out.println("the total result: " + ans);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }, "sum");

        th3.start();
        th1.start();
        th2.start();

        try {
            Thread.sleep(3000);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("over");

    }

    @Test
    public void TestBlockingQueue(){
        Model model = new BlockingQueueModel(1);
        for (int i = 0; i<2;i++){
            new Thread(model.newRunnableConsumer()).start();
        }
        for (int i = 0; i < 5; i++) {
            new Thread(model.newRunnableProducer()).start();
        }
    }

    @Test
    public void TestWaitNotifyModel(){
        Model model = new WaitNotifyModel(1);
        for (int i = 0; i < 2; i++) {
            new Thread(model.newRunnableConsumer()).start();
        }
        for (int i = 0; i < 5; i++) {
            new Thread(model.newRunnableProducer()).start();
        }
    }

    @Test
    public void TestLockConditionModel(){
        Model model = new LockConditionModel(1);
        for (int i = 0; i < 2; i++) {
            new Thread(model.newRunnableConsumer()).start();
        }
        for (int i = 0; i < 5; i++) {
            new Thread(model.newRunnableProducer()).start();
        }
    }
    @Test
    public void TestLockConditionModel2(){
        Model model = new LockConditionModel2(1);
        for (int i = 0; i < 2; i++) {
            new Thread(model.newRunnableConsumer()).start();
        }
        for (int i = 0; i < 5; i++) {
            new Thread(model.newRunnableProducer()).start();
        }
    }

    @Test
    public void TestNodeList(){
        LockConditionModel2.Buffer<String> stringBuffer = new LockConditionModel2.Buffer<>();
        stringBuffer.offer("a");
        stringBuffer.offer("b");
        stringBuffer.offer("c");
        stringBuffer.offer("d");
        stringBuffer.offer("e");

        System.out.println( "链表的长度：" +stringBuffer.getCount());

        for (int i = 0; i < stringBuffer.getCount();i++){
           String s = stringBuffer.poll();
            System.out.println(s);
        }
    }
}
